package com.hzhr.iot.user.sdk.service;

import com.hzhr.iot.core.constant.Common_ErrorsEnum;
import com.hzhr.iot.core.dto.ResponseExceptionDto;
import com.hzhr.iot.core.exception.ServiceException;
import com.hzhr.iot.core.util.HzhrHttpUtil;
import com.hzhr.iot.core.util.HzhrJsonUtil;
import com.hzhr.iot.user.sdk.constant.User_ErrorsEnum;
import com.hzhr.iot.user.sdk.model.user_moreCompleteRegistInfo.User_MoreCompleteRegistInfoGetModel;
import com.hzhr.iot.user.sdk.model.user_moreCompleteRegistInfo.User_MoreCompleteRegistInfoModel;
import com.hzhr.iot.user.sdk.model.user_moreCompleteRegistInfo.User_MoreCompleteRegistInfoPutModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;

@Service
public class User_User_MoreCompleteRegistInfoService {
    @Autowired
    RestTemplate restTemplate;

    /**
     * 用户完善注册信息 <br/><br/>
     * 简介&nbsp;:&nbsp;更新用户信息；为用户分配角色；重新登录 <br/>
     * 步骤&nbsp;: <br/>
     * 1.&nbsp;查询用户&nbsp;:&nbsp;(√) <br/>
     * &nbsp;根据路径参数userId查询用户(USER)user_1； <br/>
     * 2.&nbsp;判断用户状态&nbsp;:&nbsp;(√) <br/>
     * &nbsp;如果user_1.isNewUser为否，抛出异常201； <br/>
     * 3.&nbsp;验证公司&nbsp;:&nbsp;(√) <br/>
     * &nbsp;根据body.companyId查询公司company_1，如果company_1不存在，则抛出异常202； <br/>
     * 4.&nbsp;更新用户信息&nbsp;:&nbsp;(√) <br/>
     * &nbsp;根据body更新user_1的相关属性，同时设置以下属性： <br/>
     * &nbsp;(1)&nbsp;将user_1.isNewUser设置为否； <br/>
     * &nbsp;(2)&nbsp;user_1.type的设置规则：如果user_1是company_1的创建人，将user_1.type设置为公司创始人，否则设置为公司用户； <br/>
     * &nbsp;(3)&nbsp;user_1.deptId的设置规则：如果user_1是company_1的创建人，查询company_1下“level=1的部门”(DEPT)dept_1，user_1.deptId&nbsp;=&nbsp;dept_1.id； <br/>
     * 5.&nbsp;为用户分配角色&nbsp;: <br/>
     * &nbsp;如果user_1是company_1的创建人： <br/>
     * &nbsp;如果company_1.type为燃气公司，则在用户角色关系(USER_IN_ROLE)中添加user_1和“id=1的角色”的对应关系， <br/>
     * &nbsp;如果company_1.type为物联网设备公司，则在用户角色关系(USER_IN_ROLE)中添加user_1和“id=3的角色”的对应关系， <br/>
     * &nbsp;否则： <br/>
     * &nbsp;在用户角色关系(USER_IN_ROLE)中添加user_1和“id=6的角色”的对应关系； <br/>
     * 6.&nbsp;提交以上事务 <br/>
     * 7.&nbsp;登录&nbsp;:&nbsp;(√) <br/>
     * &nbsp;使用user_1.userName和user_1.password调用登录服务(login-service:&nbsp;/tokens&nbsp;GET)进行登录； <br/>
     * 8.&nbsp;返回信息&nbsp;:&nbsp;(√) <br/>
     * &nbsp;根据条件参数查询更新结果，组装响应并返回； <br/>
     * @param header <br/>
     * _token : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 令牌;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户代码;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户名;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-company-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 公司编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-dept-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 部门编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE');<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易流水号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易类型;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-access-mode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 访问模式 0:一般 1:全局;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 0;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param query <br/>
     * _show : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 展示形式 one:展示一个,list:展示列表,null(空值):不展示;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _show_info : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否展示详情;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/><br/>
     * @param userId 用户编号 <br/><br/>
     * @param body <br/><br/>
     * @throws ServiceException <br/>
     * @see User_MoreCompleteRegistInfoPutModel body <br/>
     * @see User_MoreCompleteRegistInfoGetModel result <br/>
     */ 
    public User_MoreCompleteRegistInfoGetModel putByUserId(Map<String, String> header, Integer userId, Map<String, Object> query, User_MoreCompleteRegistInfoPutModel body) throws ServiceException {
        HttpHeaders headers = new HttpHeaders();
        if (header == null) header = new HashMap<>();
        for (Map.Entry<String, String> entry:
        header.entrySet()) {
            headers.add(entry.getKey(), entry.getValue());
        }
        headers.add("Content-Type", "application/json;charset-utf-8");
        headers.add("accept", "application/json");
        HttpEntity httpEntity = new HttpEntity<>(body, headers);

        Map<String, Object> uriVariables = new HashMap<>();
        uriVariables.put("userId", userId);

        try {
            ResponseEntity<User_MoreCompleteRegistInfoGetModel> result = restTemplate.exchange("http://user-service/users/{userId}/more-complete-regist-infos?" + HzhrHttpUtil.queryMap2url(query), HttpMethod.PUT, httpEntity, User_MoreCompleteRegistInfoGetModel.class, uriVariables);
            return result.getBody();
        } catch (HttpClientErrorException e) {
            e.printStackTrace();
            if (e.getStatusCode().equals(HttpStatus.UNAUTHORIZED)) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_2"));
            }
            ResponseExceptionDto response = HzhrJsonUtil.json2object(e.getResponseBodyAsString(), ResponseExceptionDto.class);
            Integer code = response.getCode();
            if (code <= 100) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_" + code));
            } else {
                 throw new ServiceException(User_ErrorsEnum.getEnumByName("USER_MORECOMPLETEREGISTINFOSERVICE_PUT_BY_USER_ID_" + code));
            }
        } catch (HttpServerErrorException e) {
            e.printStackTrace();
            ResponseExceptionDto response = HzhrJsonUtil.json2object(e.getResponseBodyAsString(), ResponseExceptionDto.class);
            Integer code = response.getCode();
            if (code <= 100) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_" + code));
             } else {
                 throw new ServiceException(User_ErrorsEnum.getEnumByName("USER_MORECOMPLETEREGISTINFOSERVICE_PUT_BY_USER_ID_" + code));
             }
        } catch (Throwable e) {
              e.printStackTrace();
              throw e;
        }
    }
}

